<template>
    <!-- 编辑弹窗 -->
    <el-dialog v-model="store.state.dialogVisible" title="编辑信息">
        <el-form :model="store.state.editForm">
            <el-form-item label="用户ID" :label-width="formLabelWidth">
                <el-input v-model="store.state.editForm.id" autocomplete="off" disabled />
            </el-form-item>
            <el-form-item label="用户名" :label-width="formLabelWidth">
                <el-input v-model="store.state.editForm.username" autocomplete="off" />
            </el-form-item>
            <el-form-item label="性别" :label-width="formLabelWidth">
                <el-input v-model="store.state.editForm.sex" autocomplete="off" />
            </el-form-item>
            <el-form-item label="手机" :label-width="formLabelWidth">
                <el-input v-model="store.state.editForm.telephone" autocomplete="off" />
            </el-form-item>
            <el-form-item label="个人签名" :label-width="formLabelWidth">
                <el-input v-model="store.state.editForm.sign" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handlecancel">取消</el-button>
                <el-button type="primary" @click="handelsubmit">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { defineEmits } from 'vue';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';
import http from '../util/api';

const store = useStore();
console.log(!store.state.dialogVisible);

const formLabelWidth = '140px';

// 父组件重新获取信息
const emit = defineEmits(['getList']);

// 确认修改
const handelsubmit = () => {
  http(`/userlist/${store.state.editForm.id}`, 'put', store.state.editForm).then((res: { status: number; }) => {
    if (res.status === 200) {
      ElMessage({
        type: 'success',
        message: '修改成功！',
      });

      emit('getList');
    } else {
      ElMessage({
        type: 'error',
        message: '修改失败！',
      });
    }
    store.commit('changeDialog');
  });
};
// 取消修改
const handlecancel = () => {
  store.commit('changeDialog');
  ElMessage({
    type: 'info',
    message: '已取消！',
  });
};

</script>
<style scoped>
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>
